<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
<#assign base=request.contextPath  />
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${base}/js/layui/css/layui.css"  media="all">
    <script type="text/javascript" src="${base}/js/zhang.js"></script>
</head>
<style type="text/css">
    form{
        padding: 20px;
    }
    .layui-btn-radius{
        border-radius: 5px;

    }
    #ban{
        display: inline;
    }

</style>
<body>
<input type="hidden" id="homeId">
<div class="layui-tab">
    <ul class="layui-tab-title">
        <li class="layui-this">房间状态</li>
        <li id="ban">办理入住</li>
        <li>充值</li>
        <li>房间标签属性</li>
        <li class="l on" >退房</li>
        <li>房间档期</li>
        <li>额外消费</li>
        <li class="l">月子餐单</li>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
            <form class="layui-form" action="">
                <div class="layui-inline" >
                    <label class="layui-form-label" >当前状态</label>
                    <div class="layui-inline">
                        <input class="layui-input" disabled="true" id="creeState" style="width: 185px;">
                    </div><br /><br /><br />
                    <label class="layui-form-label">房间状态</label>
                    <div class="layui-input-inline">
                        <select name="modules" lay-verify="required" id="HomeStates" lay-search="">
                        </select>
                    </div><br /><br />
                    <div style="margin-top: 50px;margin-left: 320px;">
                        <button class="layui-btn layui-btn-normal" type="button" onclick="updateState()">保存</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="layui-tab-item" >
            <div class="layui-form" style="text-align:center;margin-top:130px;">
                <button type="button"  class="layui-btn layui-btn-normal" onclick="toOrder()">办理入住</button>
            </div>
        </div>
        <div class="layui-tab-item">
            <form class="layui-form">
                <div class="layui-inline">
                    <div class="layui-inline">
                        <label class="layui-form-label" lay-verify="required|number">金额</label>
                        <div class="layui-input-inline">
                            <input name="number" autocomplete="off" id="overMoney" class="layui-input" type="text">
                        </div>
                    </div>
                </div><br /><br /><br />
                <div class="layui-inline">
                    <label class="layui-form-label">支付渠道</label>
                    <div class="layui-input-inline">
                        <select name="modules" lay-verify="required" id="payWay" lay-search="">

                        </select>
                    </div><br />
                    <div style="margin-top: 50px;margin-left: 320px;">
                        <button type="button" onclick="saveOverMoney()" class="layui-btn layui-btn-normal">保存</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="layui-tab-item" style="padding:60px">
            <div class="layui-form">
                <a href="" id="homeLabel">该房间地理位置：</a></br></br></br>

                <a href="" id="homeMeal">该房间已预定套餐： </a>
            </div>
        </div>
        <div class="layui-tab-item">
            <form class="layui-form" action="">

                <div class="layui-inline"  style="margin: 30px;">
                    该房间充值：<a id="overMoenyText"></a>元<br/>
                    该房间除套餐外另消费共计： <a id="consumptionText"></a>元<br/>
                    该房间预付： <a id="payText"></a>元<br/>
                    该房间押金：<a id="depositText"></a>元<br/>
                    该房间定金：<a id="setMoenyText"></a>元<br/>
                    余额：<a id="receivableText"></a>元<br/>
                    <div style="margin-top: 50px;margin-left: 320px;" id="overHomeDiv">

                    </div>
                </div>
            </form>
        </div>

        <div class="layui-tab-item">
            <div class="layui-form">
                <div class="layui-form-item">
                    <label>日期范围</label>
                    <div class="layui-inline">
                        <input class="layui-input test-item  " id="stratTime" lay-verify="required" autocomplete="off" placeholder="yyyy-MM-dd" type="text" style="width: 150px;">
                    </div>
                    <div class="layui-inline">
                        <input class="layui-input test-item  " lay-verify="required" autocomplete="off" id="endTime" placeholder="yyyy-MM-dd" type="text" style="width: 150px;">
                    </div>
                    <div class="layui-inline">
                        <input class="layui-btn layui-btn-normal" type="button" onclick="saveHomeTime()" value="保存" />
                    </div>
                </div>
                <table class="layui-hide" id="test"></table>
            </div>
        </div>
        <div class="layui-tab-item">
            <form class="layui-form">
                <div class="layui-inline">
                    <div class="layui-inline">
                        <label class="layui-form-label" lay-verify="required|number">价格</label>
                        <div class="layui-input-inline">
                            <input name="" autocomplete="off" id="pirce" class="layui-input" type="text">
                        </div>
                    </div>
                </div><br /><br />
                <div class="layui-inline">
                    <div class="layui-inline">
                        <label class="layui-form-label" lay-verify="required">消费类型</label>
                        <div class="layui-input-inline">
                            <textarea name="number" autocomplete="off" style="height: 100px;width: 400px" id="payType" class="layui-input" ></textarea>
                        </div>
                    </div>
                </div><br /><br />
                <div class="layui-inline">
                    <label class="layui-form-label">支付渠道</label>
                    <div class="layui-input-inline">
                        <select name="modules" lay-verify="required" id="payWay2" lay-search="">

                        </select>
                    </div><br />
                    <div style="margin-top: 50px;margin-left: 320px;">
                        <button type="button" onclick="payEWsub()" class="layui-btn layui-btn-normal">保存</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="layui-tab-item">
            <input type="hidden" id="homeID" value="${homeId}">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">周期</label>
                        <div class="layui-input-inline">
                            <select name="modules" lay-verify="required" class=" isNull" id="weedId" lay-search="">
                                <option value="">直接选择或搜索选择</option>
                            </select>
                        </div>

                        <label class="layui-form-label">餐点选择</label>
                        <div class="layui-input-inline">
                            <select name="modules"  id="timeId" lay-search="">
                                <option value="">直接选择或搜索选择</option>
                                <option value="早点">早点</option>
                                <option value="午点">午点</option>
                                <option value="晚点">晚点</option>
                                <option value="中餐">中餐</option>
                                <option value="晚餐">晚餐</option>
                            </select>
                        </div><br/><br/><br/>

                        <label class="layui-form-label">菜品</label>
                        <div class="layui-input-inline"style="width:410px">
                            <select name="modules"  id="varietyId" lay-search="">
                                <option value="">直接选择或搜索选择</option>
                            </select>
                        </div>
                        <button class="layui-btn layui-btn-radius" lay-submit lay-filter="formDemo" type="button" onclick="subFood()" style="background-color: #0092EF;">保存</button>
                    </div>
                    <table
                            class="layui-table"
                            lay-data="{url:'${base}/foodRelation/findFoodData',
                        id:'test3',
                        where:{'homeId':$('#homeID').val()},
                        id:'testReload' ,
                        height:200,
                        width:650,
                        page: {
                        layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] ,
                        theme: '#0092EF',limit:20}
                        }"
                            lay-filter="test3">
                        <thead>
                        <tr>
                            <th lay-data="{field:'eachtime',width:60,align:'center'}">餐点</th>
                            <th lay-data="{field:'foodName',width:200}">菜品</th>
                            <th lay-data="{field:'weedName'}">周期</th>
                        </tr>
                        </thead>
                    </table>
                </div>
            </form>
        </div>
    </div>









    <script src="${base}/js/layui/layui.js" charset="utf-8"></script>
    <script src="${base}/js/jquery.min.js"></script>
    <script>

        layui.use(['form','element','laydate','table'], function(){
            var form = layui.form;
            var laydate = layui.laydate;
            var table = layui.table;
            var layer = layui.layer;
            var element = layui.element;
            table.render({
                elem: '#test'
                ,url:'${base}/roomTime/selectHomeTime'
                ,width:660
                ,height:250
                ,cols: [[
                    {field:'id',templet:function(val){
                            return   val.LAY_INDEX
                        }, title: '序号',algin:'center',width:60}
                    ,{field:'startTime',templet:function(val){
                            return changeDateFormat(val.startTime)
                        },title: '预定时间',algin:'center',width:200}
                    ,{field:'endTime',templet:function(val){
                            return changeDateFormat(val.endTime)
                        },title: '结束时间',algin:'center',width:200}
                    ,{field:'createTime',templet:function(val){
                            return changeDateFormat(val.createTime)
                        }, title: '该条记录创建时间',algin:'center',width:200}
                ]],
                where:{"homeId":$("#homeID").val()}
                ,page: {
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    ,theme: '#0092EF'
                    ,limit:5
                    ,limits:[5]
                    ,

                }
            });
            lay('.test-item').each(function(){
                laydate.render({
                    elem: this
                    ,trigger: 'click'
                });
            });

            var active = {
                tabChange: function(){
                    element.tabChange('demo', '11');
                }
            };




        });
        findState();
        findPayWay();
        setTimeout(function(){

            homeMeal();
            findAllMoneyByHomeId();
        },200);




        //查询字典表状态
        function findState(){
            var bas='${base}';
            var homeState=   getDataByCode(bas,"home_stuts");
            for(var i=0;i<homeState.length;i++){
                $("#HomeStates").append(
                        '<option value="'+homeState[i].id+'">'+homeState[i].name+'</option>'
                )
            }

        }
        //查询字典表支付渠道
        function findPayWay(){
            var bas='${base}';
            var homeState=   getDataByCode(bas,"pay");

            for(var i=0;i<homeState.length;i++){
                $("#payWay").append(
                        '<option value="'+homeState[i].id+'">'+homeState[i].name+'</option>'
                );
                $("#payWay2").append(
                        '<option value="'+homeState[i].id+'">'+homeState[i].name+'</option>'
                )
            }

        }

        //修改房间状态
        function updateState(){
            var requestData={};
            requestData.id=$("#homeID").val();
            requestData.roomStatus=$("#HomeStates").val();
            $.ajax({
                type : "POST", //提交方式
                data:{"param":JSON.stringify(requestData)},
                url : "${base}/room/updateHomeState",//路径
                success : function(data) {//返回数据根据结果进行相应的处理
                    layer.msg(data);
                    setTimeout(function(){
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    },200);
                    parent.initHome();
                }
            });
        }
        //补缴尾款
        function saveOverMoney(){
            var requestData={};
            requestData.payChannel=$("#payWay").val();
            requestData.overpay=$("#overMoney").val();
            $.ajax({
                type : "POST", //提交方式
                data:{"param":JSON.stringify(requestData),"homeId":$("#homeID").val()},
                url : "${base}/room/addMoney",//路径
                success : function(data) {//返回数据根据结果进行相应的处理
                    layer.msg(data);
                    setTimeout(function(){
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    },500);
                    parent.initHome();
                }
            });

        }

        function homeMeal(){
            $.ajax({
                type : "POST", //提交方式
                data:{"homeId":$("#homeID").val()},
                url : "${base}/room/findAllMealNameByHomeId",//路径
                success : function(data) {//返回数据根据结果进行相应的处理
                    if(data.length>1){
                        layer.alert(data[0].name+"房间出现异常，有多个订单的存在，请联系管理员")
                    }
                    $("#homeMeal").append(data[0].mealName);

                }
            });
        }

        //查询出该房间未退房的订单 所有消费明细
        function findAllMoneyByHomeId(homeId){


            $.ajax({
                type : "POST", //提交方式
                url : "${base}/room/findAllMoneyByHomeId",//路径
                data :{"homeId":$("#homeID").val()},
                async:false,
                success : function(data) {//返回数据根据结果进行相应的处理
                    //如果不为空的情况下 也就是有订单的情况
                    if (data){

                        $("#overHomeDiv").html(
                                '<button class="layui-btn layui-btn-normal" type="button" onclick="overHome()">退房</button>'
                        );
                        if(typeof(data.overpay)==="undefined"){
                            data.overpay=0;
                        }else{
                            //根据id来判断   有人的时候没有办理入住,没人的时候显示办理入住
                            document.getElementById("ban").style.display="none";
                        }

                        $("#overMoenyText").text(data.overpay);
                        if(typeof(data.consumption)==="undefined"){
                            data.consumption=0;
                        }
                        if(typeof(data.pay)==="undefined"){
                            data.pay=0;
                        }
                        if(typeof(data.deposit)==="undefined"){
                            data.deposit=0;
                        }
                        if(typeof(data.set_money)==="undefined"){
                            data.set_money=0;
                        }
                        /*   if(typeof(data.project_type)==="undefined"){
                               data.project_type=0;
                           }*/
                        $("#consumptionText").text(data.consumption);
                        $("#payText").text(data.pay);
                        $("#depositText").text(data.deposit);
                        $("#setMoenyText").text(data.set_money);
                        if(typeof(data.receivable)==="undefined"){
                            //计算余额
                            var receivable=(Number(data.overpay)+Number(data.pay)+Number(data.deposit)+Number(data.set_money))-Number(data.consumption);
                            $("#receivableText").text(receivable);
                        }else{
                            $("#receivableText").text(Number(data.receivable));
                        }
                        if($("#receivableText").text()<800){
                            layer.msg("该房间余额即将不足，当前余额为："+$("#receivableText").text()+"元，请提醒客户及时充值")
                        }
                    }
                }
            });
        }

        //退房
        function overHome(){
            var homeId=$("#homeID").val();
            layer.confirm("确定要退房吗？<br/><a style='color: red'>(该操作不可逆转)</a>", {btn: ['退了退了', '算了算了'], title: "警告！"}, function () {
                layer.msg("退房中，请稍后....");
                setTimeout(function(){
                    $.ajax({
                        type : "POST", //提交方式
                        url : "${base}/room/overHome",//路径
                        data :{"homeId":homeId},
                        async:false,
                        success : function(data) {//返回数据根据结果进行相应的处理
                            layer.msg(data);
                            parent.initHome();
                            //关闭弹出层
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            },800);
                        }
                    });

                },500);

            })

        }

        //保存新的房间档期
        function saveHomeTime(){
            if($("#stratTime").val()==null||$("#stratTime").val()==''){
                layer.msg("请选择日期");
                return;
            }
            if($("#endTime").val()==null||$("#endTime").val()==''){
                layer.msg("请选择日期");
                return;
            }
            var staTime=new Date($("#stratTime").val()).getTime();
            var enTime=new Date($("#endTime").val()).getTime();
            var requestJson={};
            requestJson.startTime=staTime;
            requestJson.endTime=enTime;
            requestJson.roomId=$("#homeId").val();
            $.ajax({
                type : "POST", //提交方式
                url : "${base}/roomTime/insertRoomTime",//路径
                data :{"param":JSON.stringify(requestJson)},
                async:false,
                success : function(data) {//返回数据根据结果进行相应的处理
                    layer.msg(data);
                    var table = layui.table;
                    //表刷新方法
                    table.reload('test', {
                        url:'${base}/roomTime/selectHomeTime'
                        ,   where:{"homeId":$("#homeId").val()} //设定异步数据接口的额外参数
                        // ,height: 300
                    });
                }
            });
        }
        //获取添加时候的周期
        var bas = "${base}"
        //home:变量  = zhang.js里边封装的名称(路径,"字典表里边的code")
        var home_data = getDataByCode(bas,"LiliMenu")
        for(var i=0;i<home_data.length;i++){
            $("#weedId").append(
                    '<option value="'+home_data[i].id+'">'+home_data[i].name+'</option>'
            )
        }
        //获取菜品
        $.ajax({
            type:"POST",
            url:"${base}/variety/selectAllVariety",
            async:false,//把异步改为同步
            success : function(data){
                for(var k=0;k<data.length;k++){
                    $("#varietyId").append(
                            '<option value="'+data[k].id+'">'+data[k].vegetable+'</option>'
                    )
                }
            }
        });


        function subFood(){

            var requestData={};
            requestData.roomId=$("#homeID").val();
            requestData.weedId=$("#weedId").val();
            requestData.eachtime=$("#timeId").val();
            requestData.food=$("#varietyId").val();
            $.ajax({
                type:"POST",
                url:"${base}/foodRelation/insertFood",
                data:{"param":JSON.stringify(requestData)},
                success : function(data){
                    layer.msg(data);
                    var table = layui.table;
                    //表刷新方法
                    table.reload('testReload', {
                        url: '${base}/foodRelation/findFoodData'
                        , where:{'homeId':$('#homeID').val()}//设定异步数据接口的额外参数
                    });
                }
            });
        }

        function payEWsub(){
            if($("#pirce").val()==""){
                layer.msg("价格不能为空")
                return
            }
            if($("#payType").val()==""){
                layer.msg("消费类型不能为空")
                return
            }

            var requestData={};
            $.ajax({
                type:"POST",
                url:"${base}/moenyDetailed/insertCharge",
                data:{"money":$("#pirce").val(),"projectType":$("#payType").val(),"homeId":$("#homeID").val(),"pay_channel":$("#payWay2").val()},
                success : function(data){
                    layer.msg(JSON.parse(data).msg);
                    setTimeout(function(){
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    },800)



                }
            });
        }

        function toOrder(){
            setTimeout(function(){
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            },100)
            setTimeout(function(){
                parent.window.location.href='${base}/orderVip/order_vip?flag=home_true&homeId='+$("#homeID").val()
            },300)

        }
    </script>
</body>
</html>